﻿using ChuanYe.CountyFireManagement.EF.MySQL.Models;
using ChuanYe.CountyFireManagement.Model;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Transactions;

namespace ChuanYe.CountyFireManagement.BLL
{
    public class FireMisreportBLL
    {
        #region 构造单例
        private FireMisreportBLL() { }
        private static FireMisreportBLL _instance;
        public static FireMisreportBLL Instance
        {
            get
            {
                return _instance ?? (_instance = new FireMisreportBLL());
            }
        }
        #endregion

        public ResultDto<List<FireMQTTResponseModel>> Apply(Firemisreport info)
        {

            ResultDto<List<FireMQTTResponseModel>> result = new ResultDto<List<FireMQTTResponseModel>>();
            var fireInfo = BLL.FireInfoBLL.Instance.Get(info.FireId);
            if (fireInfo == null)
            {
                result.Status = -1;
                result.Message = "FireInfo Is Not Exist";
                return result;
            }
            var lastRecordInfo = BLL.RecordBLL.Instance.GetLast(info.FireId, null);
            if (lastRecordInfo == null
                || (lastRecordInfo.FirstOrDefault().Type < (int)RecordType.火灾首报))
            {
                result.Status = -2;
                result.Message = "FireInfo Status Is Not Added";
                return result;
            }

            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                using (TransactionScope trans = new TransactionScope())
                {
                    ef.Firemisreport.Add(info);
                    ef.Entry<Firemisreport>(info).State = EntityState.Added;
                    ef.SaveChanges();
                    var userInfo = ef.Userinfo.Where(p => p.UserId == info.UserId).FirstOrDefault();
                    //处置记录
                    Record record = new Record();
                    record.FireId = fireInfo.FireId;
                    record.HandleTime = info.TransDatetime;
                    record.Type = (int)RecordType.误报申请;
                    record.UserId = info.UserId;
                    record.Content = string.Format(FireInfoTool.GetFireInfoRecordFormat(record.Type.Value), info.MisreportContent);
                    record.AddTime = info.TransDatetime;
                    record.Ip = info.Ip;
                    record.Applicant = userInfo.RealName;
                    
                    record.CustomData = info.MisreportId.ToString();
                    ef.Record.Add(record);
                    ef.Entry<Record>(record).State = EntityState.Added;


                    //消息
                    Message message = new Message();
                    message.FireId = fireInfo.FireId;
                    message.Type = (int)MessageType.误报申请;
                    string happenDatetime = info.TransDatetime.ToString("yyyy-MM-dd HH:mm:ss");
                    message.Content = string.Format(FireInfoTool.GetFireInfoMessageFormat(message.Type),
                                                              fireInfo.FireName, happenDatetime, userInfo.RealName, info.MisreportContent);
                    message.SendType = (int)MessageSendType.下级向上级发送;
                    message.IsRead = 0;
                    message.HandleTime = info.TransDatetime;
                    message.SendUserId = info.UserId;
                    message.Ip = fireInfo.Ip;
                    message.CustomData = info==null ||info.MisreportId==0?string.Empty:info.MisreportId.ToString();
                    ef.Message.Add(message);
                    ef.Entry<Message>(message).State = EntityState.Added;

                    ef.SaveChanges();
                    trans.Complete();

                    #region 装载消息
                    List<Message> messages = new List<Message>() { message };
                    result.FirstParam = MessageBLL.Instance.TransMessageToFireMQTTResponseModel(messages, record.Type.Value);
                    #endregion
                    return result;
                }
            }
        }


        public ResultDto<List<FireMQTTResponseModel>> Reply(int misreportId, int replyUserId,int replyResult, string replyContent,DateTime replyTime)
        {
            ResultDto<List<FireMQTTResponseModel>> result = new ResultDto<List<FireMQTTResponseModel>>();
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                using (TransactionScope trans = new TransactionScope())
                {
                    var firemisreportExist = ef.Firemisreport.Where(p => p.MisreportId == misreportId).FirstOrDefault();
                    if (firemisreportExist == null)
                    {
                        result.Status = -2;
                        result.Message = "Firemisreport Is Not Exist";
                        return result;
                    }
                    var fireInfo = BLL.FireInfoBLL.Instance.Get(firemisreportExist.FireId);
                    if (fireInfo == null)
                    {
                        result.Status = -1;
                        result.Message = "FireInfo Is Not Exist";
                        return result;
                    }

                    firemisreportExist.ReplyContent = replyContent;
                    firemisreportExist.ReplyUserId = replyUserId;
                    firemisreportExist.ReplyTime = replyTime;
                    firemisreportExist.ReplyResult = replyResult;
                    ef.Firemisreport.Add(firemisreportExist);
                    ef.Entry<Firemisreport>(firemisreportExist).State = EntityState.Modified;
                    if (replyResult == 1)
                    {
                        //关闭火灾
                        fireInfo.IsState = 1;
                        ef.Fireinfo.Add(fireInfo);
                        ef.Entry<Fireinfo>(fireInfo).State = EntityState.Modified;
                    }

                    //处置记录
                    Record record = new Record();
                    record.FireId = fireInfo.FireId;
                    record.HandleTime = firemisreportExist.ReplyTime.Value;
                    record.Type = (int)RecordType.误报审批;
                    record.UserId = firemisreportExist.UserId;
                    record.Content = string.Format(FireInfoTool.GetFireInfoRecordFormat(record.Type.Value), fireInfo.FireName, firemisreportExist.ReplyContent);
                    record.AddTime = firemisreportExist.ReplyTime;
                    record.Ip = firemisreportExist.Ip;
                    record.Applicant = fireInfo.Alarm;
                   
                    record.CustomData = firemisreportExist.MisreportId.ToString();
                    ef.Record.Add(record);
                    ef.Entry<Record>(record).State = EntityState.Added;

                    var userInfo = ef.Userinfo.Where(p => p.UserId == firemisreportExist.UserId).FirstOrDefault();
                    //消息
                    Message message = new Message();
                    message.FireId = fireInfo.FireId;
                    message.Type = (int)MessageType.误报审批;
                    string happenDatetime = firemisreportExist.ReplyTime.Value.ToString("yyyy-MM-dd HH:mm:ss");
                    string tempRr = replyResult==0 ? "不同意" : "同意";
                    message.Content = string.Format(FireInfoTool.GetFireInfoMessageFormat(message.Type),
                                                              firemisreportExist.ReplyContent, tempRr);
                    message.SendType = (int)MessageSendType.上级向下级发送;
                    message.IsRead = 0;
                    message.HandleTime = firemisreportExist.ReplyTime.Value;
                    message.SendUserId = 0;
                    message.Ip = fireInfo.Ip;
                    message.CustomData = firemisreportExist.MisreportId.ToString();
                    ef.Message.Add(message);
                    ef.Entry<Message>(message).State = EntityState.Added;
                    ef.SaveChanges();
                    trans.Complete();

                    #region 装载消息
                    List<Message> messages = new List<Message>() { message };
                    result.FirstParam = MessageBLL.Instance.TransMessageToFireMQTTResponseModel(messages, record.Type.Value);
                    #endregion
                    return result;
                }
            }
           
        }


    }
}
